//
//  CapsuleViewController.m
//  WhereToTravel
//
//  Created by dlios on 15/10/17.
//  Copyright © 2015年 zhoushengmin. All rights reserved.
//

#import "CapsuleViewController.h"
#import "CityModel.h"
#import "CapCell.h"
#import "CapsuleListViewController.h"

@interface CapsuleViewController ()<UITableViewDataSource,UITableViewDelegate,DZNEmptyDataSetDelegate,DZNEmptyDataSetSource>

@property (nonatomic, strong) NSMutableArray *capArr;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UILabel *titleLabel;

@end

@implementation CapsuleViewController

// 视图的出现将tabbar隐藏
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}

- (void)creatData {
    [ZSMAFNetWork geturl:self.url body:nil result:ZSMJSON headerFile:nil success:^(id result) {
        NSArray *arr = result[@"data"];
        NSLog(@"%@",arr);
        self.capArr = [NSMutableArray array];
        for (NSDictionary *dic in arr) {
            CityModel *city = [[CityModel alloc]initWithDic:dic];
            [self.capArr addObject:city];
        }
        [self.tableView reloadData];
        self.hud.hidden = YES;
    } failure:^(NSError *error) {
        NSLog(@"**%@",error);
        
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(night) name:@"night" object:nil];

    [self creatData];
    self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100 * WIDTH, 30 * HEIGHT)];
    self.titleLabel.textColor = [UIColor whiteColor];
    self.titleLabel.text = @"微锦囊";
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.font = [UIFont boldSystemFontOfSize:20];
    self.navigationItem.titleView = self.titleLabel;
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH * 375, HEIGHT * 610) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [UIView new];
    self.tableView.emptyDataSetDelegate = self;
    self.tableView.emptyDataSetSource = self;
    self.tableView.rowHeight = HEIGHT * 300;
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(self.view);
    }];
    
    self.hud = [MBProgressHUD showHUDAddedTo:self.tableView animated:YES];
    [self.hud hide:YES afterDelay:2];
    
    // Do any additional setup after loading the view.
}
- (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView {
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, self.view.bounds.size.height/2, WIDTH*375 , 30)];
    label.text = @"! 抱歉,没有内容";
    label.textAlignment = NSTextAlignmentCenter ;
    label.textColor = [UIColor redColor];
    
    return label;
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   
    return self.capArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *reuse = @"cell";
    CapCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[CapCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor clearColor];
    }
    CityModel *city = self.capArr[indexPath.row];
    [cell.topImage sd_setImageWithURL:[NSURL URLWithString:city.photo] placeholderImage:[UIImage imageNamed:@"F"]];
    [cell.littleImage sd_setImageWithURL:[NSURL URLWithString:city.avatar] placeholderImage:[UIImage imageNamed:@"F"]];
    cell.numLabel.text = city.count;
    cell.locaLabel.text = @"目的地";
    cell.userLabel.text = city.username;
    cell.titleLabel.text = city.title;
 //description是关键字
    cell.contentLabel.text = city.descriPtion ;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // 去除阴影效果
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    CityModel *city = self.capArr[indexPath.row];
    
    CapsuleListViewController *listVC = [[CapsuleListViewController alloc]init];
   
    listVC.listStr = [city.mId description];
    [self.navigationController pushViewController:listVC animated:YES];
}

- (void)night {
    if ([SingletonNight shareSingletonNight].isSelete == YES) {
        self.tableView.backgroundColor = [UIColor blackColor];
        self.titleLabel.textColor = [UIColor blackColor];
    }
    else {
        self.tableView.backgroundColor = [UIColor whiteColor];
        self.titleLabel.textColor = [UIColor whiteColor];
    }
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"night" object:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
